home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / Inserters.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.4 KB  |  61 lines

  1. //: C20:Inserters.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Different types of iterator inserters
  7. #include <iostream>
  8. #include <vector>
  9. #include <deque>
  10. #include <list>
  11. #include <iterator>
  12. using namespace std;
  13.  
  14. int a[] = { 1, 3, 5, 7, 11, 13, 17, 19, 23 };
  15.  
  16. template<class Cont>
  17. void frontInsertion(Cont& ci) {
  18.   copy(a, a + sizeof(a)/sizeof(int), 
  19.     front_inserter(ci));
  20.   copy(ci.begin(), ci.end(),
  21.     ostream_iterator<int>(cout, " "));
  22.   cout << endl;
  23. }
  24.  
  25. template<class Cont>
  26. void backInsertion(Cont& ci) {
  27.   copy(a, a + sizeof(a)/sizeof(int), 
  28.     back_inserter(ci));
  29.   copy(ci.begin(), ci.end(),
  30.     ostream_iterator<int>(cout, " "));
  31.   cout << endl;
  32. }
  33.  
  34. template<class Cont>
  35. void midInsertion(Cont& ci) {
  36.   typename Cont::iterator it = ci.begin();
  37.   it++; it++; it++;
  38.   copy(a, a + sizeof(a)/(sizeof(int) * 2),
  39.     inserter(ci, it));
  40.   copy(ci.begin(), ci.end(),
  41.     ostream_iterator<int>(cout, " "));
  42.   cout << endl;
  43. }
  44.  
  45. int main() {
  46.   deque<int> di;
  47.   list<int>  li;
  48.   vector<int> vi;
  49.   // Can't use a front_inserter() with vector
  50.   frontInsertion(di);
  51.   frontInsertion(li);
  52.   di.clear();
  53.   li.clear();
  54.   backInsertion(vi);
  55.   backInsertion(di);
  56.   backInsertion(li);
  57.   midInsertion(vi);
  58.   midInsertion(di);
  59.   midInsertion(li);
  60. } ///:~
  61.